#!/bin/bash
# 服务监控脚本
# 检查关键服务是否运行，如果未运行则尝试重启并发送告警

set -e

# 配置
LOG_FILE="/var/log/nanchang-tour/monitor.log"
ALERT_EMAIL=""  # 可选：告警邮箱

# 要监控的服务
SERVICES=(
    "nanchang-tour"
    "celery-worker"
    "celery-beat"
    "nginx"
    "mysql"
    "redis-server"
)

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a $LOG_FILE
}

# 检查服务状态
check_service() {
    local service=$1
    if systemctl is-active --quiet $service; then
        return 0  # 运行中
    else
        return 1  # 未运行
    fi
}

# 重启服务
restart_service() {
    local service=$1
    log "尝试重启服务: $service"
    systemctl restart $service
    sleep 3
    
    if check_service $service; then
        log "服务 $service 重启成功"
        return 0
    else
        log "错误: 服务 $service 重启失败"
        return 1
    fi
}

# 发送告警（可选）
send_alert() {
    local message=$1
    log "告警: $message"
    
    # 如果有配置邮箱，可以发送邮件
    # if [ -n "$ALERT_EMAIL" ]; then
    #     echo "$message" | mail -s "系统告警" $ALERT_EMAIL
    # fi
}

# 主监控逻辑
main() {
    log "开始服务监控检查..."
    
    local failed_services=()
    
    for service in "${SERVICES[@]}"; do
        if ! check_service $service; then
            log "警告: 服务 $service 未运行"
            failed_services+=($service)
            
            # 尝试重启
            if restart_service $service; then
                log "服务 $service 已恢复"
            else
                send_alert "服务 $service 无法启动，需要人工干预"
            fi
        fi
    done
    
    if [ ${#failed_services[@]} -eq 0 ]; then
        log "所有服务运行正常"
    else
        log "发现 ${#failed_services[@]} 个服务异常: ${failed_services[*]}"
    fi
    
    log "监控检查完成"
}

# 执行监控
main

